package com.juzipi.demo.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.juzipi.demo.pojo.Users;
import com.juzipi.demo.service.UsersService;
import com.juzipi.demo.tools.Result;
import com.juzipi.demo.utils.JwtUtil;
import com.juzipi.demo.utils.RedisUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
public class LoginController {


    @Autowired
    private UsersService usersService;


    @PostMapping("login")
    public Result login(@RequestParam("username")String username,
                        @RequestParam("password")String password
    ){
        Users users = usersService.login(username, password);
        if (StringUtils.checkValNull(users)){
            return Result.fail(404,"登陆失败,无此用户");
        }
        long currentTimeMillis = System.currentTimeMillis();
        String token = JwtUtil.getToken(users.getUsername(), currentTimeMillis);
        RedisUtil.set(username,currentTimeMillis,JwtUtil.REFRESH_EXPIRE_TIME);
        return Result.success("登录成功",token);

    }

    @DeleteMapping("/logout")
    public Result logout(HttpServletRequest httpServletRequest){
        //取出token，获取到用户名
        String token = httpServletRequest.getHeader("token");
        String username = JwtUtil.getUsername(token);
        //redis根据用户名删除
        RedisUtil.del(username);
        return Result.successMessage("退出成功");
    }


    @PostMapping("register")
    public Result register(@RequestParam(value = "username")String username,
                           @RequestParam(value = "password")String password
    ){
        if (username == null || password == null){
            return Result.fail(404,"注册失败");
        }
        Integer register = usersService.register(username, password);
        if (register == 0){
            return Result.fail(500,"注册失败");
        }
        return Result.success("注册成功",null);
    }


//    @GetMapping("user")
//    public Result getAllUser(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
//                             @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize
//    ){
//        PageResult<Users> pageResult = usersService.queryAllUser();
//        if (CollectionUtils.isEmpty(pageResult.getItems())){
//            return Result.fail(404,"NOT_FOUND");
//        }
//        return Result.success("查询成功",pageResult);
//    }


    @RequiresRoles(logical = Logical.OR,value = {"user","admin","super"})
    @GetMapping("test")
    public Result test(){

        return Result.successMessage("\"user\",\"admin\",\"super\"访问成功啦");
    }



    @RequiresRoles(logical = Logical.OR,value = {"admin","super"})
    @GetMapping("test1")
    public Result test1(){

        return Result.successMessage("\"admin\",\"super\"访问成功啦");
    }



    @RequiresRoles(logical = Logical.OR,value = {"admin","super"})
    @RequiresPermissions(logical = Logical.OR,value = {"manage","any"})
    @GetMapping("test2")
    public Result test2(){

        return Result.successMessage("只有角色为admin或者super并且拥有any或者manage权限才能访问 访问成功啦");
    }


    @RequiresRoles(logical = Logical.OR,value = {"admin","super"})
    @RequiresPermissions(value = {"any"})
    @GetMapping("test3")
    public Result test3(){

        return Result.successMessage("只有角色为admin或者super并且拥有any权限才能访问 访问成功啦");
    }


    @GetMapping("test4")
    public Result test4(){
        return Result.success("任何人都可以访问 成功",null);
    }

}
